﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DevExpress.XtraReports.Web;

public partial class WebUserControl_ControlXemBaoCao : System.Web.UI.UserControl
{
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    BCDataCenter.DataAccessLayer.DAL dal;
    MethodClassCommon common;
    DataTable dt, dt1;
    SqlDataAdapter da;
    SqlConnection con;
    static string usertype = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                MultiView1.ActiveViewIndex = 0;
                displayTTPP();
                displayVung();
                displayKieuBaoCao();
                displayTop();
                TextBox1.Text = DateTime.Now.ToString("dd/MM/yyyy");
                usertype = checkUserType();
                try
                {
                    DropTTPP.SelectedValue = Session["Sitecode"].ToString();
                    if ((usertype == "sa") || (usertype == "us"))
                    {
                        DropTTPP.Enabled = false;
                    }

                }
                catch (Exception ex)
                {
                    uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
                }

            }
            else
            {
                HienThiReport();
            }
        }
        catch(Exception ex)
        {
            uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
        }
    }
    void displayKieuBaoCao()
    {
        DropBaoCao.Items.Add("Báo cáo phân tích kinh doanh");
        DropBaoCao.Items.Add("BUM");
        DropBaoCao.Items.Add("Doanh số VNĐ");
        DropBaoCao.Items.Add("Doanh số số lượng");

    }
    void displayTTPP()
    {
       
        dal = new BCDataCenter.DataAccessLayer.DAL();
        dt = dal.get_data_table("GET_SITENAME_ALL");
        DropTTPP.DataSource = dt;
        DropTTPP.DataTextField = "sitename";
        DropTTPP.DataValueField = "Sitecode";
        DropTTPP.DataBind();
       
    }
    void displayVung()
    {
        dal = new BCDataCenter.DataAccessLayer.DAL();
        dt1 = dal.get_data_table("GET_VUNG");
        DropVung.DataSource = dt1;
        DropVung.DataTextField = "region";
        DropVung.DataValueField = "region";
        DropVung.DataBind();
    }
    public string checkUserType()
    {
        common = new MethodClassCommon();
        string type = common.GetDataDepend("GET_USERTYPE", "UserName", Session["username"].ToString());
        return type;
    }
    void displayTop()
    {
        for (int i = 1; i < 201; i++)
        {
            DropTop.Items.Add(Convert.ToString(i));
        }
    }
    void BindReport(string sitecode, string vung, string fromdate, string todate)
    {
        try
        {
            common = new MethodClassCommon();
            con = new SqlConnection(str);
            if (DropBaoCao.SelectedItem.ToString() == "Báo cáo phân tích kinh doanh")
            {
                    ReportAllBC rp = new ReportAllBC();
                    da = new SqlDataAdapter("P_Get_dailysaleKPI", con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@sitecode", sitecode));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@Region", vung));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@fromdate", fromdate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@todate", todate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@intTop", "5"));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@username", Session["username"].ToString()));
                    dt = new DataTable();
                    da.Fill(dt);
                    rp.Parameters["ttpp"].Value = DropTTPP.SelectedItem.ToString();
                    rp.Parameters["vung"].Value = vung;
                    rp.Parameters["fromdate"].Value = fromdate;
                    rp.Parameters["todate"].Value = todate;
                    rp.DataSource = dt;
                    ReportViewer1.Report = rp;
                    ReportViewer1.DataBind();
                   
            }
            else if (DropBaoCao.SelectedItem.ToString() == "BUM")
            {
                ReportTop20BUM rp = new ReportTop20BUM();
                if ((sitecode == "ALL")&& (vung!="ALL")) 
                {
                   
                    da = new SqlDataAdapter("get_Top20BumAllSitecode", con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@Region", vung));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@fromdate", fromdate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@todate", todate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@intTop",DropTop.SelectedItem.ToString()));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@username", Session["username"].ToString()));
                    dt = new DataTable();
                    da.Fill(dt);
                    rp.Parameters["ttpp"].Value = "ALL";

                }
                else if ((sitecode == "ALL") && (vung == "ALL"))
                {
                    
                    da = new SqlDataAdapter("get_Top20BumAllRegionSitecode", con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@fromdate", fromdate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@todate", todate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@intTop", DropTop.SelectedItem.ToString()));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@username", Session["username"].ToString()));
                    dt = new DataTable();
                    da.Fill(dt);
                    rp.Parameters["ttpp"].Value = "ALL";
                }
                else if ((vung == "ALL") && (sitecode!="ALL"))
                {
                    
                    da = new SqlDataAdapter("get_Top20BumAllRegion", con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@sitecode", sitecode));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@fromdate", fromdate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@todate", todate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@intTop", DropTop.SelectedItem.ToString()));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@username", Session["username"].ToString()));
                    dt = new DataTable();
                    da.Fill(dt);
                    rp.Parameters["ttpp"].Value = common.GetDataDepend("GET_SiteName_Address", "sitecode", sitecode);
                }
                else
                {
                   
                    da = new SqlDataAdapter("get_Top20Bum", con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@sitecode", sitecode));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@Region", vung));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@fromdate", fromdate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@todate", todate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@intTop", DropTop.SelectedItem.ToString()));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@username",Session["username"].ToString()));
                    dt = new DataTable();
                    da.Fill(dt);
                    rp.Parameters["ttpp"].Value = common.GetDataDepend("GET_SiteName_Address", "sitecode", sitecode);
                }

                rp.Parameters["vung"].Value = vung;
                rp.Parameters["fromdate"].Value = fromdate;
                rp.Parameters["todate"].Value = todate;
                rp.DataSource = dt;
                ReportViewer1.Report = rp;
                ReportViewer1.DataBind();
               

            }
            else if (DropBaoCao.SelectedItem.ToString() == "Doanh số VNĐ")
            {
               
                ReportTop5BySale rp = new ReportTop5BySale();
                if ((sitecode == "ALL") && (vung!="ALL"))
                {
                    
                    da = new SqlDataAdapter("get_Top5ProductsBySalesAllSitecode", con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@Region", vung));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@fromdate", fromdate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@todate", todate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@intTop", Convert.ToInt32(DropTop.SelectedItem.ToString())));
                    dt = new DataTable();
                    da.Fill(dt);
                    rp.Parameters["ttpp"].Value = "ALL";

                }
                else if ((sitecode == "ALL") && (vung == "ALL"))
                {
                    da = new SqlDataAdapter("get_Top5ProductsBySalesAllRegionSitecode", con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@fromdate", fromdate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@todate", todate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@intTop", Convert.ToInt32(DropTop.SelectedItem.ToString())));
                    dt = new DataTable();
                    da.Fill(dt);
                    rp.Parameters["ttpp"].Value = "ALL";
                }
                else if ((vung == "ALL") && (sitecode!="ALL"))
                {
                    da = new SqlDataAdapter("get_Top5ProductsBySalesAllRegion", con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@sitecode", sitecode));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@fromdate", fromdate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@todate", todate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@intTop", Convert.ToInt32(DropTop.SelectedItem.ToString())));
                    dt = new DataTable();
                    da.Fill(dt);
                    rp.Parameters["ttpp"].Value = common.GetDataDepend("GET_SiteName_Address", "sitecode", sitecode);
                }
                else
                {
                    da = new SqlDataAdapter("get_Top5ProductsBySales", con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@sitecode", sitecode));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@Region", vung));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@fromdate", fromdate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@todate", todate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@intTop", Convert.ToInt32(DropTop.SelectedItem.ToString())));
                    dt = new DataTable();
                    da.Fill(dt);
                    rp.Parameters["ttpp"].Value = common.GetDataDepend("GET_SiteName_Address", "sitecode", sitecode);
                   
                }
                rp.Parameters["vung"].Value = vung;
                rp.Parameters["fromdate"].Value = fromdate;
                rp.Parameters["todate"].Value = todate;
                rp.DataSource = dt;
                ReportViewer1.Report = rp;
                ReportViewer1.DataBind();
               
            }
            else if (DropBaoCao.SelectedItem.ToString() == "Doanh số số lượng")
            {
                ReportTop5ByUnit rp = new ReportTop5ByUnit();
                if ((sitecode == "ALL") && (vung!="ALL"))
                {
                  
                    da = new SqlDataAdapter("get_Top5ProductsSepByUnitsAllSitecode", con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@Region", vung));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@fromdate", fromdate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@todate", todate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@intTop", Convert.ToInt32(DropTop.SelectedItem.ToString())));
                    dt = new DataTable();
                    da.Fill(dt);
                    rp.Parameters["ttpp"].Value = "ALL";

                }
                else if ((sitecode == "ALL") && (vung == "ALL"))
                {
                  
                    da = new SqlDataAdapter("get_Top5ProductsSepByUnitsAllRegionSitecode", con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@fromdate", fromdate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@todate", todate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@intTop", Convert.ToInt32(DropTop.SelectedItem.ToString())));
                    dt = new DataTable();
                    da.Fill(dt);
                    rp.Parameters["ttpp"].Value = "ALL";
                }
                else if ((vung == "ALL")&&(sitecode!="ALL")) 
                {
                    da = new SqlDataAdapter("get_Top5ProductsSepByUnitsAllRegion", con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@sitecode", sitecode));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@fromdate", fromdate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@todate", todate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@intTop", Convert.ToInt32(DropTop.SelectedItem.ToString())));
                    dt = new DataTable();
                    da.Fill(dt);
                    rp.Parameters["ttpp"].Value = common.GetDataDepend("GET_SiteName_Address", "sitecode", sitecode);
                }
                else
                {
                    da = new SqlDataAdapter("get_Top5ProductsSepByUnits", con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@sitecode", sitecode));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@Region", vung));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@fromdate", fromdate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@todate", todate));
                    da.SelectCommand.Parameters.Add(new SqlParameter("@intTop", Convert.ToInt32(DropTop.SelectedItem.ToString())));
                    dt = new DataTable();
                    da.Fill(dt);
                    rp.Parameters["ttpp"].Value = common.GetDataDepend("GET_SiteName_Address", "sitecode", sitecode);
                }
                rp.Parameters["vung"].Value = vung;
                rp.Parameters["fromdate"].Value = fromdate;
                rp.Parameters["todate"].Value = todate;
                rp.DataSource = dt;
                ReportViewer1.Report = rp;
                ReportViewer1.DataBind();
              
    
            }
              
        }
        catch(Exception ex)
        {
            uscMsgBox1.AddMessage(ex.Message,uscMsgBox.enmMessageType.Error);
        }
    }
    void HienThiReport()
    {
        try
        {
            string fromdate = "01/" + TextBox1.Text.Substring(3);
            BindReport(DropTTPP.SelectedValue.ToString().Trim(), DropVung.SelectedItem.ToString().Trim(), fromdate, TextBox1.Text);
            if (dt.Rows.Count > 0)
            {
                MultiView1.ActiveViewIndex = 1;
            }
            else
            {
                MultiView1.ActiveViewIndex = 0;
                uscMsgBox1.AddMessage("không có dữ liệu", uscMsgBox.enmMessageType.Error);
            }
        }
        catch (Exception e)
        {
            uscMsgBox1.AddMessage(e.Message, uscMsgBox.enmMessageType.Error);
        }
    }
    protected void ButtonXem_Click(object sender, EventArgs e)
    {
         HienThiReport();
    }
}
